﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Maticsoft.Common;
using System.Drawing;
using LTP.Accounts.Bus;
using System.Reflection;
using XDSoft.Model;
namespace XDSoft.Web.TB_Clients
{
    public partial class List : Page
    {

        XDSoft.BLL.TB_Clients bll = new XDSoft.BLL.TB_Clients();

        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!Page.IsPostBack)
            {

                XDSoft.BLL.TB_Options bll = new XDSoft.BLL.TB_Options();
                this.ddlClientLevel.AppendDataBoundItems = true;
                this.ddlClientLevel.Items.Add(new ListItem("", ""));
                this.ddlClientLevel.DataSource = bll.GetModelList("parent='" + EnumCommon.OptionsType.ClientLevel.ToString() + "'");
                this.ddlClientLevel.DataTextField = "name";
                this.ddlClientLevel.DataValueField = "id";
                this.ddlClientLevel.DataBind();

                gridView.BorderColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_bordercolorlight"].ToString());
                gridView.HeaderStyle.BackColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_titlebgcolor"].ToString());
                btnDelete.Attributes.Add("onclick", "return confirm(\"你确认要删除吗？\")");
                BindData();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            bll.DeleteList(idlist);
            BindData();
        }

        #region gridView

        private string getWhere()
        {
            StringBuilder strWhere = new StringBuilder();
            if (!string.IsNullOrEmpty(this.txtAddress.Text))
            {
                if (strWhere.Length > 0)
                    strWhere.AppendLine(" and ");
                strWhere.AppendFormat("address like '%{0}%'", this.txtAddress.Text);
            }

            if (!string.IsNullOrEmpty(this.txtClientName.Text))
            {
                if (strWhere.Length > 0)
                    strWhere.AppendLine(" and ");
                strWhere.AppendFormat("clientName like '%{0}%'", this.txtClientName.Text);
            }

            if (!string.IsNullOrEmpty(this.txtCorpName.Text))
            {
                if (strWhere.Length > 0)
                    strWhere.AppendLine(" and ");
                strWhere.AppendFormat("corpName like '%{0}%'", this.txtCorpName.Text);
            }

            if (!string.IsNullOrEmpty(this.txtMobile.Text))
            {
                if (strWhere.Length > 0)
                    strWhere.AppendLine(" and ");
                strWhere.AppendFormat("(mobile1 like '%{0}%' or mobile2 like '%{1}%')", this.txtMobile.Text, this.txtMobile.Text);
            }

            if (!string.IsNullOrEmpty(this.txtTel.Text))
            {
                if (strWhere.Length > 0)
                    strWhere.AppendLine(" and ");
                strWhere.AppendFormat("tel like '%{0}%'", this.txtTel.Text);
            }

            if (!string.IsNullOrEmpty(this.ddlClientLevel.SelectedValue))
            {
                if (strWhere.Length > 0)
                    strWhere.AppendLine(" and ");
                strWhere.AppendFormat("clientLevel='{0}'", this.ddlClientLevel.SelectedValue);
            }

            return strWhere.ToString();
        }

        public void BindData()
        {
            string strWhere = getWhere();
            
            DataSet ds = bll.GetList(strWhere.ToString());
            gridView.DataSource = ds;
            if (ds != null && ds.Tables.Count > 0)
            {
                XDSoft.BLL.TB_Options oBll = new XDSoft.BLL.TB_Options();

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (ds.Tables[0].Rows[i]["clientLevel"] != null && !string.IsNullOrEmpty(ds.Tables[0].Rows[i]["clientLevel"].ToString()))
                        ds.Tables[0].Rows[i]["clientLevel"] = oBll.GetModel(Convert.ToInt32(ds.Tables[0].Rows[i]["clientLevel"])).name;
                }
            }
            gridView.DataBind();
            ViewState["QueryDataSet"] = ds;
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("style", "background:#FFF");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");

                //object obj1 = DataBinder.Eval(e.Row.DataItem, "Levels");
                //if ((obj1 != null) && ((obj1.ToString() != "")))
                //{
                //    e.Row.Cells[1].Text = obj1.ToString();
                //}

            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion

        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (ViewState["QueryDataSet"] != null)
            {
                DataSet ds = (DataSet)ViewState["QueryDataSet"];
                if (ds != null && ds.Tables.Count > 0)
                {
                    DataSet newDs = new DataSet();
                    string tableName = "sheet1";
                    DataTable dt = WebCommon.ChangeDataTableFieldAttrName(typeof(Model.TB_Clients), ds.Tables[0], tableName);
                    newDs.Tables.Add(dt);
                    ExcelHelper.ToExcel(newDs, tableName + ".xls", this.Page.Response);
                }
            }
        }
    }
}
